#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h> // 图片
#include <SDL_mixer.h> // 音频
#include <SDL_ttf.h> // 文字

#include "Bullet.h"
#include "include/DrawUtils.h"
#include "include/Content.h"
#include "include/Atlas.h"
#include "include/SceneManager.h"
#include "GameScene.h"
#include "PlayerSelectionScene.h"
#include "MainMenuScene.h"
#include "PeashooterPlayer.h"
#include "Platform.h"
#include "Player.h"
#include "SunflowerPlayer.h"

#undef main

#define GAME_TITLE "PlantsVSZombiesV1"

const char* Content::WINDOW_TITLE = "PlantsVSZombiesV1";
int Content::WINDOW_WIDTH = 1280;
int Content::WINDOW_HEIGHT = 720;
bool Content::WINDOW_QUIT_FLAG = false;
int Content::GAME_FPS = 60;

SDL_Window* mainWindow;
SDL_Renderer* mainRenderer;

// 游戏场景相关
SceneManager* sceneManager;
Scene* mainMenuScene;
Scene* playerSelectionScene;
Scene* gameScene;
// 相机
Camera camera;

// 游戏资源相关
// 加载音频资源
Mix_Music* musicBgmGame;
Mix_Music* musicBgmMenu;

// 加载绘制资源
SDL_Texture* player1p = nullptr;
SDL_Texture* player1pCursor = nullptr;
SDL_Texture* player1pDesc = nullptr;
SDL_Texture* player1pSelectorBtnDown = nullptr;
SDL_Texture* player1pSelectorBtnIdle = nullptr;
SDL_Texture* player1pWinner = nullptr;

SDL_Texture* player2p = nullptr;
SDL_Texture* player2pCursor = nullptr;
SDL_Texture* player2pDesc = nullptr;
SDL_Texture* player2pSelectorBtnDown = nullptr;
SDL_Texture* player2pSelectorBtnIdle = nullptr;
SDL_Texture* player2pWinner = nullptr;
SDL_Texture* avatarPeashooter = nullptr;
SDL_Texture* avatarSunflower = nullptr;
SDL_Texture* gravestone = nullptr;
SDL_Texture* hills = nullptr;
SDL_Texture* menuBackground = nullptr;
SDL_Texture* pea = nullptr;
SDL_Texture* selectorBackground = nullptr;
SDL_Texture* selectorTip = nullptr;
SDL_Texture* sky = nullptr;
SDL_Texture* vs = nullptr;
SDL_Texture* winnerBar = nullptr;
SDL_Texture* platformLarge = nullptr;
SDL_Texture* platformSmall = nullptr;
SDL_Texture* sunflowerSelectorBackground = nullptr;
SDL_Texture* peashooterSelectorBackground = nullptr;

Atlas* sunflowerIdleRightAtlas = nullptr;
Atlas* sunflowerIdleLeftAtlas = nullptr;
Atlas* peashooterIdleRightAtlas = nullptr;
Atlas* peashooterIdleLeftAtlas = nullptr;

Atlas* jumpEffectAtlas = nullptr;
Atlas* landEffectAtlas = nullptr;
Atlas* peaBreakAtlas = nullptr;
Atlas* peashooterDieRightAtlas = nullptr;
Atlas* peashooterDieLeftAtlas = nullptr;
Atlas* peaShooterAttackExRightAtlas = nullptr;
Atlas* peaShooterAttackExLeftAtlas = nullptr;
Atlas* peaShooterRunRightAtlas = nullptr;
Atlas* peaShooterRunLeftAtlas = nullptr;

Atlas* runEffectAtlas = nullptr;
Atlas* sunAtlas = nullptr;
Atlas* sunExAtlas = nullptr;
Atlas* sunExplodeAtlas = nullptr;
Atlas* sunExExplodeAtlas = nullptr;
Atlas* sunTextAtlas = nullptr;
Atlas* sunflowerAttackExRightAtlas = nullptr;
Atlas* sunflowerAttackExLeftAtlas = nullptr;
Atlas* sunflowerDieRightAtlas = nullptr;
Atlas* sunflowerDieLeftAtlas = nullptr;
Atlas* sunflowerRunRightAtlas = nullptr;
Atlas* sunflowerRunLeftAtlas = nullptr;

TTF_Font* font = nullptr;
SDL_Texture* fontTexture = nullptr;
// 是否是调试模式
bool IS_DEBUG_MODE = false;
// 地图
std::vector<Platform*> platforms;
Platform* platform_small_1 = nullptr;
Platform* platform_small_2 = nullptr;
Platform* platform_large = nullptr;
// 子弹列表
std::vector<Bullet*> bullets;
// 玩家
Player* player01 = nullptr;
Player* player02 = nullptr;

SDL_Texture* player01Avatar = nullptr;
SDL_Texture* player02Avatar = nullptr;

void loadResources() {
    sunflowerIdleRightAtlas = new Atlas();
    sunflowerIdleLeftAtlas = new Atlas();
    peashooterIdleRightAtlas = new Atlas();
    peashooterIdleLeftAtlas = new Atlas();

    jumpEffectAtlas = new Atlas();
    landEffectAtlas = new Atlas();
    peaBreakAtlas = new Atlas();
    peashooterDieRightAtlas = new Atlas();
    peashooterDieLeftAtlas = new Atlas();
    peaShooterAttackExRightAtlas = new Atlas();
    peaShooterAttackExLeftAtlas = new Atlas();
    peaShooterRunRightAtlas = new Atlas();
    peaShooterRunLeftAtlas = new Atlas();

    runEffectAtlas = new Atlas();
    sunAtlas = new Atlas();
    sunExAtlas = new Atlas();
    sunExplodeAtlas = new Atlas();
    sunExExplodeAtlas = new Atlas();
    sunTextAtlas = new Atlas();
    sunflowerAttackExRightAtlas = new Atlas();
    sunflowerAttackExLeftAtlas = new Atlas();
    sunflowerDieRightAtlas = new Atlas();
    sunflowerDieLeftAtlas = new Atlas();
    sunflowerRunRightAtlas = new Atlas();
    sunflowerRunLeftAtlas = new Atlas();

    pea = loadImage("./assets/pea.png");
    player1p = loadImage("./assets/1p.png");
    player1pCursor = loadImage("./assets/1p_cursor.png");
    player1pDesc = loadImage("./assets/1p_desc.png");
    player1pSelectorBtnDown = loadImage("./assets/1p_selector_btn_down.png");
    player1pSelectorBtnDown = loadImage("./assets/1p_selector_btn_idle.png");
    player1pWinner = loadImage("./assets/1p_winner.png");

    player2p = loadImage("./assets/2p.png");
    player2pCursor = loadImage("./assets/2p_cursor.png");
    player2pDesc = loadImage("./assets/2p_desc.png");
    player2pSelectorBtnDown = loadImage("./assets/2p_selector_btn_down.png");
    player2pSelectorBtnDown = loadImage("./assets/2p_selector_btn_idle.png");
    player2pWinner = loadImage("./assets/2p_winner.png");

    avatarPeashooter = loadImage("./assets/avatar_peashooter.png");
    avatarSunflower = loadImage("./assets/avatar_sunflower.png");
    gravestone = loadImage("./assets/gravestone.png");
    hills = loadImage("./assets/hills.png");
    menuBackground = loadImage("./assets/menu_background.png");
    selectorBackground = loadImage("./assets/selector_background.png");
    selectorTip = loadImage("./assets/selector_tip.png");
    sky = loadImage("./assets/sky.png");
    vs = loadImage("./assets/vs.png");
    winnerBar = loadImage("./assets/winner_bar.png");
    platformLarge = loadImage("./assets/platform_large.png");
    platformSmall = loadImage("./assets/platform_small.png");
    sunflowerSelectorBackground = loadImage("./assets/sunflower_selector_background.png");
    peashooterSelectorBackground = loadImage("./assets/peashooter_selector_background.png");

    jumpEffectAtlas->loadFromFile("./assets/jump_effect_%d.png", 5);
    landEffectAtlas->loadFromFile("./assets/land_effect_%d.png", 2);
    peaBreakAtlas->loadFromFile("./assets/pea_break_%d.png", 3);
    runEffectAtlas->loadFromFile("./assets/run_effect_%d.png", 4);
    sunAtlas->loadFromFile("./assets/sun_%d.png", 5);
    sunExAtlas->loadFromFile("./assets/sun_ex_%d.png", 5);
    sunExplodeAtlas->loadFromFile("./assets/sun_explode_%d.png", 5);
    sunExExplodeAtlas->loadFromFile("./assets/sun_ex_explode_%d.png", 5);
    sunTextAtlas->loadFromFile("./assets/sun_text_%d.png", 6);

    sunflowerAttackExRightAtlas->loadFromFile("./assets/sunflower_attack_ex_%d.png", 9);
    sunflowerAttackExLeftAtlas->loadFromFileFlip("./assets/sunflower_attack_ex_%d.png", 9);
    sunflowerDieRightAtlas->loadFromFile("./assets/sunflower_die_%d.png", 2);
    sunflowerDieLeftAtlas -> loadFromFileFlip("./assets/sunflower_die_%d.png", 2);
    sunflowerRunRightAtlas->loadFromFile("./assets/sunflower_run_%d.png", 5);
    sunflowerRunLeftAtlas->loadFromFileFlip("./assets/sunflower_run_%d.png", 5);
    sunflowerIdleRightAtlas->loadFromFile("./assets/sunflower_idle_%d.png", 8);
    sunflowerIdleLeftAtlas->loadFromFileFlip("./assets/sunflower_idle_%d.png", 8);


    peashooterDieRightAtlas->loadFromFile("./assets/peashooter_die_%d.png", 3);
    peashooterDieLeftAtlas->loadFromFileFlip("./assets/peashooter_die_%d.png", 3);
    peaShooterAttackExRightAtlas->loadFromFile("./assets/peashooter_attack_ex_%d.png", 3);
    peaShooterAttackExLeftAtlas->loadFromFileFlip("./assets/peashooter_attack_ex_%d.png", 3);
    peaShooterRunRightAtlas->loadFromFile("./assets/peashooter_run_%d.png", 5);
    peaShooterRunLeftAtlas->loadFromFileFlip("./assets/peashooter_run_%d.png", 5);
    peashooterIdleRightAtlas->loadFromFile("./assets/peashooter_idle_%d.png", 8);
    peashooterIdleLeftAtlas->loadFromFileFlip("./assets/peashooter_idle_%d.png", 8);

    // 加载字体
    font = TTF_OpenFont("./assets/IPix.ttf", 28);

    // 加载音乐
    musicBgmGame = Mix_LoadMUS("./assets/bgm_game.mp3");
    musicBgmMenu = Mix_LoadMUS("./assets/bgm_menu.mp3");

    // 加载和初始化地图 1034*402
    platform_small_1 = new Platform(platformSmall, 200, 350);
    platform_small_1->setCollisionShape(platform_small_1->renderPosition.y + 15, platform_small_1->renderPosition.x, platform_small_1->renderPosition.x + platform_small_1->width);
    platform_small_2 = new Platform(platformSmall, Content::WINDOW_WIDTH - 200 - 248, 350);
    platform_small_2->setCollisionShape(platform_small_2->renderPosition.y + 15, platform_small_2->renderPosition.x, platform_small_2->renderPosition.x + platform_small_2->width);
    platform_large = new Platform(platformLarge, (Content::WINDOW_WIDTH - 1034) / 2, Content::WINDOW_HEIGHT - 252);
    platform_large->setCollisionShape(platform_large->renderPosition.y + 50, platform_large->renderPosition.x, platform_large->renderPosition.x + platform_large->width);


    platforms.push_back(platform_small_1);
    platforms.push_back(platform_small_2);
    platforms.push_back(platform_large);
}

// 资源释放
void freeResources() {
    sunflowerIdleRightAtlas->freeImages();
    sunflowerIdleLeftAtlas->freeImages();
    peashooterIdleRightAtlas->freeImages();
    peashooterIdleLeftAtlas->freeImages();
    jumpEffectAtlas->freeImages();
    landEffectAtlas->freeImages();
    peaBreakAtlas->freeImages();
    peashooterDieRightAtlas->freeImages();
    peashooterDieLeftAtlas->freeImages();
    peaShooterAttackExRightAtlas->freeImages();
    peaShooterAttackExLeftAtlas->freeImages();
    peaShooterRunRightAtlas->freeImages();
    peaShooterRunLeftAtlas->freeImages();

    runEffectAtlas->freeImages();
    sunAtlas->freeImages();
    sunExAtlas->freeImages();
    sunExplodeAtlas->freeImages();
    sunExExplodeAtlas->freeImages();
    sunTextAtlas->freeImages();

    sunflowerAttackExRightAtlas->freeImages();
    sunflowerAttackExLeftAtlas->freeImages();
    sunflowerDieRightAtlas->freeImages();
    sunflowerDieLeftAtlas->freeImages();
    sunflowerRunRightAtlas ->freeImages();
    sunflowerRunLeftAtlas->freeImages();
    // 释放子弹资源

    // 释放地图资源
    for (int i = 0 ; i < platforms.size(); i++) {
        free(platforms[i]);
    }
}

void initScenes() {
    sceneManager = new SceneManager();
    mainMenuScene = new MainMenuScene();
    playerSelectionScene = new PlayerSelectionScene();
    gameScene = new GameScene();
    sceneManager->setCurrentScene(mainMenuScene);
}

int main() {

    if (SDL_Init(SDL_INIT_EVERYTHING)) {
        printf("SDL_Init Error: %s\n", SDL_GetError());
        return -1;
    }

    mainWindow = SDL_CreateWindow(Content::WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Content::WINDOW_WIDTH, Content::WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (mainWindow == nullptr) {
        printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
        return -1;
    }

    mainRenderer = SDL_CreateRenderer(mainWindow, -1, 0);
    if (mainRenderer == nullptr) {
        printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
        return -1;
    }
    // 加载资源
    loadResources();
    // 初始化场景
    initScenes();

    SDL_Event event;
    uint32_t frameStartTicks = 0;
    uint32_t frameEndTicks = 0;
    uint32_t frameDeltaTicks = 0;

    while (!Content::WINDOW_QUIT_FLAG) {
        frameStartTicks = SDL_GetTicks();
        if (SDL_PollEvent(&event)) {
            // 退出事件
            if (event.type == SDL_QUIT) {
                Content::WINDOW_QUIT_FLAG = true;
            }
            // 键盘按键事件
            sceneManager->onInput(event);
        }
        static int lastTickTime = SDL_GetTicks();
        int currentTickTime = SDL_GetTicks();
        int delta_tick = currentTickTime - lastTickTime;
        // printf("delta_tick = %d\n", delta_tick);
        // 数据更新
        sceneManager->onUpdate(delta_tick); // 相当于固定每一帧发送一个固定的变化量的值
        lastTickTime = currentTickTime;
        // 绘制
        SDL_SetRenderDrawColor(mainRenderer, 255, 255, 255, 255);
        SDL_RenderClear(mainRenderer);
        // 渲染逻辑
        sceneManager->onDraw(camera);
        // 渲染逻辑
        SDL_RenderPresent(mainRenderer);
        frameEndTicks = SDL_GetTicks();
        frameDeltaTicks = frameEndTicks - frameStartTicks;
        if (frameDeltaTicks < (1000 / Content::GAME_FPS)) { // 如果执行没有超过帧率 1000 / FPS
            SDL_Delay((1000 / Content::GAME_FPS) - frameDeltaTicks);
        }
    }

    // 资源释放
    freeResources();
    // 主窗口资源清理
    SDL_DestroyRenderer(mainRenderer);
    SDL_DestroyWindow(mainWindow);
    SDL_Quit();
    
    return 0;
}